import java.util.Arrays;

public class Test2 {
    public static int BinartSearch(int [] array,int key){
        int left = 0;
        int right = array.length-1;
        while(left<right){
            int mid = (left+right)/2;
            if(array[mid]==key){
                return key;
            }
            else if(array[mid]<key){
                left = mid+1;
            }
            else if(array[mid]>key){
                right = mid-1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int [] array = {5,7,9,5,4,2,8,1,6};
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j]>array[j+1]){
                    int tmp = array[j];
                    array[j]=array[j+1];
                    array[j+1] = tmp;
                }
            }
        }
        System.out.println(Arrays.toString(array));
    }
    public static void main1(String[] args) {
        int [] array = {1,2,5,7,4,6,9};
        int key = 3;
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
       int c = BinartSearch(array,key);
        System.out.println(c);
    }
}
